/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BuildData;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

/**
 *
 * @author Administrator
 */
public final class PMData {

    private HashMap<String, Integer> expn_fullHm = new HashMap<String, Integer>();

    public PMData() throws FileNotFoundException, XMLStreamException, UnsupportedEncodingException, IOException {

        System.out.println("package BuildData; class PMData");

        setExpn_FullHm();
        writeToFile();
    }

    public void setExpn_FullHm() throws FileNotFoundException, XMLStreamException {
        FileInputStream fis = new FileInputStream("trainset\\hand\\expn_hand.xml");
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);

        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {
                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("W")) {
                        String atValue = xmlStreamReader.getAttributeValue(0);
                        if (atValue.equals("EXPN")) {
                            String full = xmlStreamReader.getAttributeValue(1);
                            String expn = xmlStreamReader.getElementText();
                            String expn_full = expn + "|" + full;
                            if (expn_fullHm.containsKey(expn_full)) {
                                int count = expn_fullHm.get(expn_full);
                                count++;
                                expn_fullHm.put(expn_full, count);
                            } else {
                                expn_fullHm.put(expn_full, 1);
                            }
                        }
                    }
            }
        }

    }

    public void writeToFile() throws FileNotFoundException, UnsupportedEncodingException, IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("data\\expand\\expn_full.db"), "UTF8"));
        for (Map.Entry<String, Integer> e : expn_fullHm.entrySet()) {
            bw.write(e.getKey() + "|" + e.getValue() + "\n");
        }
        bw.close();
    }
}
